Expression | Value | Expression | Value |
---|---|---|---|
25 == 25 | true | 25 != 25 | false |
25 <= 25 | true | 25 > 25 | false |
25 >= 25 | true | 25 = 25 | illegal |
-5 < 7 | true | -305 <= 97 | true |
In an if
statement,
the true or false of a boolean expression
picks whether the true branch or the false branch of code
is executed.
To practice putting together programs with two-way decisions, let us
look at another story problem.
A clothing store wants a program that calculates the tax on an item. Clothing that costs $100 or more has a 5% tax. Clothing that costs less than $100 is tax free. Write a program that asks for the price, then calculates the tax and prints it out, and prints out the total cost of the item.
For simplicity the price will be an integer.
All print statements will be placed after the if
statement.
Here is a skeleton of the program:
______________ class TaxProgram { public static void main (String[] args) throws IOException { ____________________ BufferedReader stdin = __________________________ String inData; int price; double tax ; System.out.println("Enter the price:"); __________________________ price = Integer.parseInt( inData ); if ( ________________ ) ______________________ else ________ System.out.println("Item cost: " + price + " Tax: " + tax + " Total: " + (price+tax) ); } } |
Here are some program fragments tax = price * taxRate; new BufferedReader ( new InputStreamReader( System.in ) ); inData = stdin.readLine(); double taxRate = 0.05; price >= 100 import java.io.*; tax = 0; |